<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\ExcelExporter;
use App\Dealer;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class DealersController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('经销商表')
            ->description('description')
            ->body($this->grid());
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑')
            ->description('description')
            ->body($this->form()->edit($id));
    }
    public function show($id, Content $content)
    {
        return $content
            ->header('查看详情')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增经销商')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Dealer::findOrFail($id));

        $show->id('Id');
        $show->region('营销总部');
        $show->market('销售区域');
        $show->company('公司名称');
        $show->username('账号');
        $show->password('密码');
        $show->name('负责人名');
        $show->market_level('区域等级')->display(function ($v) {
            $list = [1 => '地级', 2 => '县级'];
            return $list[$v];
        });
        $show->goods_1('浓香豆乳');
        $show->goods_2('果果昔');
        $show->goods_3('安慕希');
        $show->goods_4('金典');
        $show->goods_5('味可滋');
        $show->goods_6('ABC果浆');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Dealer);
        $grid->model()->where('is_fake', 0);
        $grid->exporter(new ExcelExporter());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('company', '公司名称');
        });
        $grid->id('Id');
        $grid->region('营销总部');
        $grid->market('销售区域');
        $grid->company('公司名称');
        $grid->username('账号');
        $grid->password('密码');
        $grid->name('负责人名');
        $grid->market_level('区域等级');
        $grid->column('合计')->display(function (){
            return $this->getAllMoneyCount();
        });
        $grid->is_localed('是否参会');
        return $grid;
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Dealer);
        $form->text('region', '营销总部');
        $form->text('market', '销售区域');
        $form->text('company', '公司名称');
        $form->text('username', '账户');
        $form->text('password', '密码');
        $form->text('name', '负责人名字');
        $form->text('is_localed', '是否参会')->help('请输入是或否');
        $form->text('market_level', '区域等级');
        $form->number('goods_1', $GLOBALS['goods'][0]->name);
        $form->number('goods_2', $GLOBALS['goods'][1]->name);
        $form->number('goods_3', $GLOBALS['goods'][2]->name);
        $form->number('goods_4', $GLOBALS['goods'][3]->name);
        $form->number('goods_5', $GLOBALS['goods'][4]->name);
        $form->number('goods_6', $GLOBALS['goods'][5]->name);
        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
